package q;

import adc.ad.top.me.Student;
import com.sun.org.apache.xml.internal.utils.res.XResourceBundle;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;

public class Test {
    public static void main(String[] args) throws Exception {
        //获取类Class的三种方式
        //1

            Class a = Class.forName("adc.ad.top.me.Student");//静态代代码块类加载时执行
                                                             //Class.forName()会类加载
            System.out.println(a.getName());


        System.out.println("======================================================");
        //2
        Student student = new Student();
        Class b = student.getClass();
        System.out.println(b);
        System.out.println( "a == b ?" +String.valueOf(a==b));//类型为Class的变量都指向方法区的同一段代码片段，所以a==b为true
        System.out.println("========================================================");
        //3
        Class c = Student.class;
        System.out.println(c.getSimpleName());
        System.out.println("========================================================");

        // 获取到类对象后可用以下方式实例化对象
        c.newInstance();//底层调用Student的无参数构造方法


        System.out.println("===============================================");
        //路径问题
        //获取绝对路径，不管是在什么环境下执行，都能找的到
        String path = Thread.currentThread().getContextClassLoader().getResource("adc/ad/top/me/Student.class").getPath();
        //但是要找的文件得在out文件夹下
        System.out.println("类"+c.getSimpleName()+"的绝对路径为:"+path);
        //直接返回一个流
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("adc/ad/top/me/Student.properties");
        //忘了有什么用
        //看了老杜的源码想起来了，可以作为配置文件的
        Properties properties = new Properties();
        //读取配置文件需要一个输入流
        properties.load(is);
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("address"));
        //如果不用这个直接获取一个流那么加载配置文件会麻烦一些
            //首先要获取配置文件的绝对路径
        String path2 = Thread.currentThread().getContextClassLoader().getResource("adc/ad/top/me/Student.properties").getPath();
        FileInputStream fis = new FileInputStream(path2);
        Properties properties1 = new Properties();
            //加载配置文件到Properties集合中，同样也可以
        properties1.load(fis);
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("address"));
        System.out.println(properties1.getProperty("aaaaa"));
        System.out.println("===================================================");
        //另外关于加载配置文件有个bug形式的存在------资源绑定器
            //只能是.properties的文件
        ResourceBundle rb = ResourceBundle.getBundle("adc/ad/top/me/Student");//一行代码就可以
        //注意事项文件名不能加.properties后缀！！！！！！！！！
        System.out.println( rb.getString("name"));
        //这就是资源绑定器的魅力吗？？


    }
}
